package io.renren.modules.sys.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.dao.ArrivalDao;
import io.renren.modules.sys.entity.ArrivalEntity;
import io.renren.modules.sys.entity.ContractEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.ArrivalService;
import io.renren.modules.sys.service.ContractService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-12-16 21:09:07
 */
@RestController
@RequestMapping("sys/contract")
public class ContractController extends AbstractController  {
    @Autowired
    private ContractService contractService;
    
    @Autowired
    private ArrivalService arrivalService;
    
    @Autowired
    private ArrivalDao arrivalDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = contractService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        ContractEntity contract = contractService.selectById(id);

        return R.ok().put("contract", contract);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ContractEntity contract){
        contractService.insert(contract);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ContractEntity contract){
        ValidatorUtils.validateEntity(contract);
        contractService.updateAllColumnById(contract);//全部更新
        
        return R.ok();
    }
    
    /**
     * 修改
     */
    @RequestMapping("/update2")
    public R update2(@RequestBody ContractEntity contract){
    	SysUserEntity sysUserEntity=getUser();
        ValidatorUtils.validateEntity(contract);
        ContractEntity contract1 = contractService.selectById(contract.getId());
        contract1.setAmountUse(new BigDecimal(contract1.getAmountUse())
        		.add(new BigDecimal(contract.getAmountUse())).toString());
        contractService.updateAllColumnById(contract1);//全部更新
        ArrivalEntity arrival=new ArrivalEntity();
        arrival.setAddTime(contract.getAddTime());
        arrival.setAmount(contract.getAmountUse());
        arrival.setAmount2(contract1.getAmount());
        Long amount=arrivalDao.getOne(contract1.getCustomerId());
        amount=amount!=null?amount:0;
        arrival.setAmount3(new BigDecimal(amount.toString())
        		.add(new BigDecimal(contract.getAmountUse())).toString());
        arrival.setContractNo(contract1.getContractNo());
        arrival.setUserId(sysUserEntity.getUserId().intValue());
        arrival.setUserName(sysUserEntity.getUsername());
        arrival.setName(contract1.getCustomerName());
        arrival.setCustomerId(contract1.getCustomerId());
        arrivalService.insert(arrival);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        contractService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
